package com.zut.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zut.dao.ExamDao;
import com.zut.dao.ExamResultDao;
import com.zut.domain.Exam;
import com.zut.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamServiceImpl implements ExamService {
    @Autowired
    private ExamDao examDao;

    @Autowired
    private ExamResultDao examResultDao;

    public PageInfo<Exam> getAll(Integer pageNum, Integer pageSize) {
        List<Exam> list;
        PageHelper.startPage(pageNum, pageSize);
        list = examDao.getAll();
        return new PageInfo<>(list);
    }

    public boolean insert(Exam exam) {
        return examDao.insert(exam) > 0;
    }

    public boolean deleteById(String id) {
        return examDao.deleteById(id) > 0;
    }

    public boolean update(Exam exam) {
        return examDao.update(exam) > 0;
    }

    public PageInfo<Exam> getByPlace(Integer pageNum, Integer pageSize, String place) {
        List<Exam> list;
        PageHelper.startPage(pageNum, pageSize);
        list = examDao.getByPlace(place);
        return new PageInfo<>(list);

    }

    public PageInfo<Exam> getBySubject(Integer pageNum, Integer pageSize, String subject) {
        List<Exam> list;
        if (subject.equals("1")) {
            subject = "科目一";
        } else if (subject.equals("2")) {
            subject = "科目二";
        } else if (subject.equals("3")) {
            subject = "科目三";
        } else
            subject = "科目四";
        PageHelper.startPage(pageNum, pageSize);
        list = examDao.getBySubject(subject);
        return new PageInfo<>(list);
    }

    public PageInfo<Exam> getExamById(Integer pageNum, Integer pageSize, String id,String date) {
        String subject = examResultDao.getSubjectById(id);
        if (subject.equals("0")) {
            subject = "科目一";
        } else if (subject.equals("1")) {
            subject = "科目二";
        } else if (subject.equals("2")) {
            subject = "科目三";
        } else if (subject.equals("3")) {
            subject = "科目四";
        } else subject = "科目一";
        List<Exam> list;
        PageHelper.startPage(pageNum, pageSize);
        date = date+'%';
        list = examDao.getExamMsg(subject,date);
        return new PageInfo<>(list);
    }

    public Exam getByExamId(String id) {
        String examId = examResultDao.getExamId(id);
        Exam byExamId = examDao.getByExamId(examId);
        return byExamId;
    }

    public Exam getOrderExam(String id) {
        String examId = "";
        examId = examResultDao.getOrderExam(id);
        Exam byExamId = examDao.getByExamId(examId);
        return byExamId;
    }

    public PageInfo<Exam> getOrderExamPage(Integer pageNum, Integer pageSize, String id){
        String examId = "";
        List<Exam> list;
        examId = examResultDao.getOrderExam(id);
        PageHelper.startPage(pageNum, pageSize);
        list = examDao.getByExamIdPage(examId);
        return new PageInfo<>(list);
    }

    public Exam getExam(String id) {
        Exam byExamId = examDao.getByExamId(id);
        return byExamId;
    }

}
